package com.qf.controller;

import com.alibaba.fastjson.JSON;
import com.qf.pojo.Product;
import com.qf.service.Impl.ProductServiceImpl;
import com.qf.service.ProductService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;


@WebServlet("/productFindAllController.action")
public class ProductFindAllController extends HttpServlet {

    private ProductService productService = new ProductServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询商品
        List<Product> productList =  productService.findAll();
        //创建Map集合
        HashMap<String, Object> map = new HashMap<>();
        //判断
        if(productList !=null && productList.size() > 0){
            //返回给前端
            map.put("data",productList);
        }
        //返回数据给前端
        resp.getWriter().write(JSON.toJSONString(map));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
